package cn.edu.scau.cmi.lixiaoyuan.client.springHibernate;

import java.util.Scanner;
import java.util.Set;

import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.edu.scau.cmi.lixiaoyuan.orm.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.lixiaoyuan.orm.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.lixiaoyuan.orm.hibernate.domain.Student;
import cn.edu.scau.cmi.lixiaoyuan.orm.hibernate.domain.Teacher;

public class SpringHibernateClient {

	  static ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-hibernate.xml");
	  static StudentDAO studentDAO = ctx.getBean(StudentDAO.class);
	  static TeacherDAO teacherDAO = ctx.getBean(TeacherDAO.class);
	  static int choose = 0;
	  static Scanner sc = new Scanner(System.in);
    //功能1：添加学生
	public static void addStudent() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		Student stu = new Student();
		
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入学生ID:");
		Long id = sc.nextLong();
        stu.setId(id);
        
        System.out.print("请输入学生Name:");
     
        String name = sc.next();
		stu.setName(name);
		System.out.print("请输入学生Number:");
		Long numb = sc.nextLong();
		stu.setNumber(numb);

		System.out.println("请输入学生Tutor:");
		Scanner sc3 = new Scanner(System.in);
		Long tutor = sc3.nextLong();
		Teacher teacher = teacherDAO.findById(tutor);
		stu.setTeacher(teacher);
		studentDAO.save(stu);

		System.out.println(studentDAO.findById(stu.getId()).toString());
		System.out.println("添加成功");
	}
//	
//	//功能2：修改学生
	public static void updateStudent() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入需要修改的学生ID:");
		Long id = sc.nextLong();
		Student stu = studentDAO.findById(id);
		System.out.print("请输入学生Name:");
        String name = sc.next();
		stu.setName(name);
		System.out.print("请输入学生Number:");
		Long numb = sc.nextLong();
		stu.setNumber(numb);
		System.out.println("请输入学生Tutor:");
		Long tutor = sc.nextLong();
		Teacher teacher = teacherDAO.findById(tutor);
		stu.setTeacher(teacher);
		studentDAO.update(stu);

		System.out.println(studentDAO.findById(stu.getId()).toString());
		System.out.println("修改成功");
	}
	//功能3：删除学生
	public static void deleteStudent() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入需要删除的学生ID:");
		Long id = sc.nextLong();
		Student stu = studentDAO.findById(id);
		studentDAO.delete(stu);

		System.out.println("删除成功");
	}
	//功能4：添加教师
	public static void addTeacher() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		
		System.out.println("请输入老师ID：");
		Scanner sc = new Scanner(System.in);
		Long id = sc.nextLong();
		Teacher tea = new Teacher();
		tea.setId(id);
		System.out.print("请输入学生Name:");
        String name = sc.next();
		tea.setName(name);
		teacherDAO.save(tea);
		System.out.println(teacherDAO.findById(tea.getId()).toString());
		System.out.println("添加成功");

	}
	//功能5：修改教师
	public static void updateTeacher() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		
		System.out.println("请输入老师ID：");
		Scanner sc = new Scanner(System.in);
		Long id = sc.nextLong();
		Teacher tea = teacherDAO.findById(id);
		tea.setId(id);
		System.out.print("请输入老师Name:");
        String name = sc.next();
		tea.setName(name);
		teacherDAO.update(tea);
		System.out.println(teacherDAO.findById(tea.getId()).toString());
		System.out.println("修改成功");

	}
	//功能6：删除教师
public static void deleteTeacher() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		
		System.out.println("请输入老师ID：");
		Scanner sc = new Scanner(System.in);
		Long id = sc.nextLong();
		Teacher tea = teacherDAO.findById(id);
		teacherDAO.delete(tea);
		System.out.println("删除成功");

		
	}
	
	//功能7：学生选择指导教师
    //功能8：学生修改指导教师
public static void updateStudentTutor() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
	System.out.println("请输入学生ID：");
	Scanner sc = new Scanner(System.in);
	Long sid = sc.nextLong();
	Student stu = studentDAO.findById(sid);
	System.out.println("请输入老师ID：");
	Long tid = sc.nextLong();
	Teacher tea = teacherDAO.findById(tid);
	stu.setTeacher(tea);
	studentDAO.update(stu);

	System.out.println("修改成功");
	
}
	
	//功能9：教师选择被指导的学生
public static void choseStudent() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
	Scanner sc = new Scanner(System.in);
	System.out.println("请输入老师ID：");
	Long tid = sc.nextLong();
	Teacher tea = teacherDAO.findById(tid);
	System.out.println("请输入学生ID：");
	Long sid = sc.nextLong();
	Student stu = studentDAO.findById(sid);
	stu.setTeacher(tea);
	studentDAO.update(stu);

	System.out.println("修改成功");
	
}
	//功能10：教师修改被指导的学生
   public static void teaUpdateStudent() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
	  
		System.out.println("请输入老师ID：");
		Long tid = sc.nextLong();
		Teacher tea = teacherDAO.findById(tid);
		Set<Student> students = tea.getStudents();
		System.out.println("以下为此指导老师的学生信息");
		for(Student stu : students) {
			System.out.println(stu.toString());
        }
		
		System.out.println("请输入学生ID：");
		Long sid = sc.nextLong();
		Student stu = studentDAO.findById(sid);
		stu.setTeacher(tea);
		studentDAO.update(stu);
		System.out.println("修改成功");
		sc.close();
   }
//	
   public static int getChoose() {
	   
		 System.out.println(
				 "1：添加学生\r\n" + 
		 		"2：修改学生\r\n" + 
		 		"3：删除学生\r\n" + 
		 		"4：添加教师\r\n" + 
		 		"5：修改教师\r\n" + 
		 		"6：删除教师\r\n" + 
		 		"7：学生选择指导教师\r\n" + 
		 		"8：学生修改指导教师\r\n" + 
		 		"9：教师选择被指导的学生\r\n" + 
		 		"10：教师修改被指导的学生\r\n" + 
		 		"0:退出"+
		 		"");
		 System.out.println("请输入需要的功能序号:");
		int chose  = sc.nextInt();
		return chose;
   }
   
	public static void main(String[] args) throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		choose = getChoose();
		while(choose!=0) {
	     switch (choose) {
	    	 case 1:
	    		 addStudent();
	    		 break;
	    	 case 2:
	    		 updateStudent();
	    		 break;
	    	 case 3:
	    		 deleteStudent();
	    		 break;
	    	 case 4:
	    		 addTeacher();
	    		 break;
	    	 case 5:
	    		 updateTeacher();
	    		 break;
	    	 case 6:
	    		 deleteTeacher();
	    		 break;
	    	 case 7:
	    		 updateStudentTutor();
	    		 break;
	    	 case 8:
	    		 updateStudentTutor();
	    		 break;
	    	 case 9:
	    		 choseStudent();
	    		 break;
	    	 case 10:
	    		 teaUpdateStudent();
	    		 break;
	    		 
	     }
	     
	     choose = getChoose();
	}
//	
	sc.close();
	
}
	
	
	
	
	
	


}
